/**
 * 
 */
package fhq.hcmute.qrcode.util;

/**
 * @author Hien Nguyen Le
 * @Email: nglehien@gmail.com
 */
public class AlphanumericTable {

	private static Alphanumeric[] alphanumerics = alphanumericTable();

	public static Alphanumeric[] alphanumericTable() {
		return new Alphanumeric[] { new Alphanumeric('0', 0),
				new Alphanumeric('1', 1), new Alphanumeric('2', 2),
				new Alphanumeric('3', 3), new Alphanumeric('4', 4),
				new Alphanumeric('5', 5), new Alphanumeric('6', 6),
				new Alphanumeric('7', 7), new Alphanumeric('8', 8),
				new Alphanumeric('9', 9), new Alphanumeric('A', 10),
				new Alphanumeric('B', 11), new Alphanumeric('C', 12),
				new Alphanumeric('D', 13), new Alphanumeric('E', 14),
				new Alphanumeric('F', 15), new Alphanumeric('G', 16),
				new Alphanumeric('H', 17), new Alphanumeric('I', 18),
				new Alphanumeric('J', 19), new Alphanumeric('K', 20),
				new Alphanumeric('L', 21), new Alphanumeric('M', 22),
				new Alphanumeric('N', 23), new Alphanumeric('O', 24),
				new Alphanumeric('P', 25), new Alphanumeric('Q', 26),
				new Alphanumeric('R', 27), new Alphanumeric('S', 28),
				new Alphanumeric('T', 29), new Alphanumeric('U', 30),
				new Alphanumeric('V', 31), new Alphanumeric('W', 32),
				new Alphanumeric('X', 33), new Alphanumeric('Y', 34),
				new Alphanumeric('Z', 35), new Alphanumeric(' ', 36),
				new Alphanumeric('$', 37), new Alphanumeric('%', 38),
				new Alphanumeric('*', 39), new Alphanumeric('+', 40),
				new Alphanumeric('-', 41), new Alphanumeric('.', 42),
				new Alphanumeric('/', 43), new Alphanumeric(':', 44) };
	}

	public static int getValueByChar(char c) {
		for (Alphanumeric i : alphanumerics) {
			if (i.character == c) {
				return i.value;
			}
		}
		return -1;
	}

	public static String getCharByValue(int v) {
		for (Alphanumeric i : alphanumerics) {
			if (i.value == v) {
				return String.valueOf(i.character);
			}
		}
		return null;
	}

	private static class Alphanumeric {
		private int value;
		private char character;

		/**
		 * @param value
		 * @param character
		 */
		public Alphanumeric(char character, int value) {
			super();
			this.value = value;
			this.character = character;
		}
	}
}
